//package com.xtr.tx.config.datasource.writer;
//
//import com.alibaba.druid.pool.DruidDataSource;
//import com.github.pagehelper.PageHelper;
//import com.lcn.core.db.LCNDataSourceProxy;
//import org.apache.ibatis.plugin.Interceptor;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.mybatis.spring.SqlSessionFactoryBean;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//import org.springframework.core.env.Environment;
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//import org.springframework.core.io.support.ResourcePatternResolver;
//import org.springframework.jdbc.core.JdbcTemplate;
//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//import tk.mybatis.spring.annotation.MapperScan;
//
//import javax.annotation.Resource;
//import javax.sql.DataSource;
//import java.sql.SQLException;
//import java.util.Properties;
//
///**
// * <p>数据源配置</p>
// *
// * @author 张峰 zfvip_it@163.com
// * @createTime: 2017/1/15 11:45
// */
//@Configuration
//@MapperScan(basePackages = WriterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "writerSqlSessionFactory")
//public class WriterDataSourceConfig {
//
//    static final String PACKAGE = "com.xtr.**.writer";
//
//    @Resource
//    private Environment environment;
//
//    @Bean(name = "writerDataSource")
//    @Primary
//    public DataSource wrtDataSource() {
//        DruidDataSource dataSource = new DruidDataSource();
//        try {
//            dataSource.setDriverClassName(environment.getProperty("writer.jdbc.driverClassName"));
//            dataSource.setUrl(environment.getProperty("writer.jdbc.url"));
//            dataSource.setUsername(environment.getProperty("writer.jdbc.username"));
//            dataSource.setPassword(environment.getProperty("writer.jdbc.password"));
//            //打开PSCache，并且指定每个连接上PSCache的大小
//            dataSource.setPoolPreparedStatements(false);
//            //初始大小
//            dataSource.setInitialSize(1);
//            //最大连接池数量
//            dataSource.setMaxActive(30);
//            //最小连接池数量
//            dataSource.setMinIdle(5);
//            //获取连接等待超时的时间
//            dataSource.setMaxWait(60000);
//            //间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
//            dataSource.setTimeBetweenEvictionRunsMillis(60000);
//            //配置一个连接在池中最小生存的时间，单位是毫秒
//            dataSource.setMinEvictableIdleTimeMillis(30000);
//            //超过时间限制是否回收
////            dataSource.setRemoveAbandoned(true);
////            //超时时间；单位为秒。180秒=3分钟
////            dataSource.setRemoveAbandonedTimeout(180);
////            dataSource.setLogAbandoned(true);
//            //建议配置为true，不影响性能，并且保证安全性。申请连接的时候检测，如果空闲时间大于timeBetweenEvictionRunsMillis，执行validationQuery检测连接是否有效。
//            dataSource.setTestWhileIdle(true);
//            //用来检测连接是否有效的sql，要求是一个查询语句。如果validationQuery为null，testOnBorrow、testOnReturn、testWhileIdle都不会其作用。
//            dataSource.setValidationQuery("SELECT 1");
//            //测试有效的超时时间
////            dataSource.setValidationQueryTimeout(1000);
//            //从池中取出连接前进行检验
////            dataSource.setTestOnBorrow(true);
//            //配置监控统计拦截的filters，去掉后监控界面sql无法统计
//            dataSource.setFilters("stat");
//            LCNDataSourceProxy lcnDataSourceProxy = new LCNDataSourceProxy();
//            //数据源
//            lcnDataSourceProxy.setDataSource(dataSource);
//            //lcn最大连接
//            lcnDataSourceProxy.setMaxCount(10);
//            return lcnDataSourceProxy;
//
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return dataSource;
//    }
//
//    @Bean(name = "writerTransactionManager")
//    @Primary
//    public DataSourceTransactionManager writerTransactionManager() {
//        return new DataSourceTransactionManager(wrtDataSource());
//    }
//
//    @Bean(name = "writerSqlSessionFactory")
//    @Primary
//    public SqlSessionFactory writerSqlSessionFactory(@Qualifier("writerDataSource") DataSource rdsDataSource) throws Exception {
//        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
//        sessionFactory.setDataSource(rdsDataSource);
//        sessionFactory.setTypeAliasesPackage("com.xtr.**.domain");
//
//        //分页插件
//        PageHelper pageHelper = new PageHelper();
//        Properties properties = new Properties();
//        properties.setProperty("reasonable", "true");
//        properties.setProperty("supportMethodsArguments", "true");
//        properties.setProperty("returnPageInfo", "check");
//        properties.setProperty("params", "count=countSql");
//        pageHelper.setProperties(properties);
//
//        //添加插件
//        sessionFactory.setPlugins(new Interceptor[]{pageHelper});
//        //添加XML目录
//        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        try {
//            sessionFactory.setMapperLocations(resolver.getResources("classpath:com/xtr/**/writer/*.xml"));
//            return sessionFactory.getObject();
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new RuntimeException(e);
//        }
//    }
//
//    @Bean(name = "writerJdbcTemplate")
//    public JdbcTemplate writerJdbcTemplate(
//            @Qualifier("writerDataSource") DataSource dataSource) {
//        return new JdbcTemplate(dataSource);
//    }
//}
